<template>
  <div class="login">
    <div class="login_box">
      <!-- logo -->
      <div class="box_logo">
        <img src="@/assets/logo.png" alt="">
      </div>
      <!-- form表单元素 -->
      <el-form class="form_content" :model="form" :rules="rules" ref="reform">
        <el-form-item prop="username">
          <el-input type="text" placeholder="请输入用户名" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="text" placeholder="请输入密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="success" @click="resetOut('reform')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      fullPath: '',
      /* 绑定form表单对象数据 */
      form: {
        username: '',
        password: ''
      },
      /* 设置规则校验 */
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 6, message: '密码长度最小3位最大6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapMutations(['getToken']),
    /* 登录 */
    login() {
      /* 校验输入框 */
      this.$refs.reform.validate(async (valid) => {
        // console.log(valid);
        if (valid) {
          /* 向后端发送请求 */
          let res = await this.$http.post('/login', this.form)
          // console.log(res);
          if (res.meta.status == 200) {
            /* 将用户信息保存到vuex中 */
            console.log(res.data.token);
            this.getToken({token:res.data.token})
            this.$message.success(res.meta.msg);
            if (this.fullPath != '/') {
              this.$router.push(this.fullPath)
            } else {
              this.$router.push('/');
            }
          } else {
            this.$message.error(res.meta.msg);
          }
        }
      })
    },
    /* 退出 */
    resetOut(reform) {
      // console.log(this)
      this.$refs[reform].resetFields();
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      console.log(vm);
      console.log(from);
      vm.fullPath = from.fullPath;
    })
  }
}
</script>
<style lang="scss">
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  min-width: 720px;
  position: relative;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    /* logo */
    .box_logo {
      width: 150px;
      height: 150px;
      background-color: #eee;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #ddd;
      border: 14px solid white;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    /* 表单元素 */
    .form_content {
      width: 100%;
      height: 160px;
      // background-color: red;
      margin-top: 100px;

      .el_form {
        width: 90%;
        margin: 0 auto;
        margin-top: 20px;
      }

      .el_form_button {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>